<?php
/**
 * Class that operate on table 'classified_questions'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class ClassifiedQuestionsMySqlDAO implements ClassifiedQuestionsDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return ClassifiedQuestionsMySql 
	 */
	public function load($classifiedQuestion){
		$sql = 'SELECT * FROM classified_questions WHERE question_id = "'.$classifiedQuestion->question_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllSearchBy($classifiedQuestion){
		$sql = 'SELECT * FROM classified_questions WHERE '.$classifiedQuestion->search_by.' like"%'.$classifiedQuestion->search_by_value.'%" order by '.$classifiedQuestion->order_by.' '.$classifiedQuestion->sort_order.' limit '.$classifiedQuestion->results_per_page.' offset '.$classifiedQuestion->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function SearchByCount($classifiedQuestion){
		$sql = 'SELECT count(*) FROM classified_questions WHERE '.$classifiedQuestion->search_by.' like"%'.$classifiedQuestion->search_by_value.'%" order by '.$classifiedQuestion->order_by.' '.$classifiedQuestion->sort_order.' limit '.$classifiedQuestion->results_per_page.' offset '.$classifiedQuestion->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	public function queryAll($classifiedQuestion){
		$sql = 'SELECT * FROM classified_questions order by '.$classifiedQuestion->order_by.' '.$classifiedQuestion->sort_order.' limit '.$classifiedQuestion->results_per_page.' offset '.$classifiedQuestion->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function queryAllCount($classifiedQuestion){
		$sql = 'SELECT count(*) FROM classified_questions order by '.$classifiedQuestion->order_by.' '.$classifiedQuestion->sort_order.' limit '.$classifiedQuestion->results_per_page.' offset '.$classifiedQuestion->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param classifiedQuestion primary key
 	 */
	public function delete($classifiedQuestion){
		$sql = 'UPDATE classified_questions SET status = "'.$classifiedQuestion->status.'" WHERE question_id = "'.$classifiedQuestion->question_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param ClassifiedQuestionsMySql classifiedQuestion
 	 */
	public function insert($classifiedQuestion){
		$sql = 'INSERT INTO classified_questions (question, user_email, classified_id, admin_id, replay, is_public, created_date, updated_date) VALUES ("'.$classifiedQuestion->question.'", "'.$classifiedQuestion->user_email.'", "'.$classifiedQuestion->classified_id.'", "'.$classifiedQuestion->admin_id.'", "'.$classifiedQuestion->replay.'", "'.$classifiedQuestion->is_public.'", "'.$classifiedQuestion->created_date.'", "'.$classifiedQuestion->updated_date.'")';
		$sqlQuery = new SqlQuery($sql);
		$id = $this->executeInsert($sqlQuery);	
		$classifiedQuestion->question_id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param ClassifiedQuestionsMySql classifiedQuestion
 	 */
	public function update($classifiedQuestion){
		$sql = 'UPDATE classified_questions SET question = ?, user_email = ?, classified_id = ?, admin_id = ?, replay = ?, is_public = ?, status = ?, updated_date = ? WHERE question_id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($classifiedQuestion->question);
		$sqlQuery->set($classifiedQuestion->user_email);
		$sqlQuery->setNumber($classifiedQuestion->classified_id);
		$sqlQuery->setNumber($classifiedQuestion->admin_id);
		$sqlQuery->set($classifiedQuestion->replay);
		$sqlQuery->setNumber($classifiedQuestion->is_public);
		$sqlQuery->setNumber($classifiedQuestion->status);
		$sqlQuery->set($classifiedQuestion->updated_date);
		$sqlQuery->setNumber($classifiedQuestion->question_id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM classified_questions';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByQuestion($value){
		$sql = 'SELECT * FROM classified_questions WHERE question = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUserEmail($value){
		$sql = 'SELECT * FROM classified_questions WHERE user_email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByClassifiedId($value){
		$sql = 'SELECT * FROM classified_questions WHERE classified_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByAdminId($value){
		$sql = 'SELECT * FROM classified_questions WHERE admin_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByReplay($value){
		$sql = 'SELECT * FROM classified_questions WHERE replay = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByIsPublic($value){
		$sql = 'SELECT * FROM classified_questions WHERE is_public = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM classified_questions WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM classified_questions WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM classified_questions WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByQuestion($value){
		$sql = 'DELETE FROM classified_questions WHERE question = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUserEmail($value){
		$sql = 'DELETE FROM classified_questions WHERE user_email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByClassifiedId($value){
		$sql = 'DELETE FROM classified_questions WHERE classified_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByAdminId($value){
		$sql = 'DELETE FROM classified_questions WHERE admin_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByReplay($value){
		$sql = 'DELETE FROM classified_questions WHERE replay = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByIsPublic($value){
		$sql = 'DELETE FROM classified_questions WHERE is_public = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM classified_questions WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM classified_questions WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM classified_questions WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return ClassifiedQuestionsMySql 
	 */
	protected function readRow($row){
		$classifiedQuestion = new ClassifiedQuestion();
		
		$classifiedQuestion->question_id = $row['question_id'];
		$classifiedQuestion->question = $row['question'];
		$classifiedQuestion->user_email = $row['user_email'];
		$classifiedQuestion->classified_id = $row['classified_id'];
		$classifiedQuestion->admin_id = $row['admin_id'];
		$classifiedQuestion->replay = $row['replay'];
		$classifiedQuestion->is_public = $row['is_public'];
		$classifiedQuestion->status = $row['status'];
		$classifiedQuestion->created_date = $row['created_date'];
		$classifiedQuestion->updated_date = $row['updated_date'];

		return $classifiedQuestion;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return ClassifiedQuestionsMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>